<%@page import="multiface.web.util.Paginator"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>

<%
    if (session.getAttribute("admin") == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<script>
    $(document).ready(function() {
        $("a.delete-user").fancybox({
            ajax:{
                type:"GET"
            }
        });
    });
</script>
<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<div class="box">
    <%
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();

        //Parametri della jsp
        String order = request.getParameter("order");
        String direction = request.getParameter("direction");
        String text = request.getParameter("text");
        String jsp = "list_user.jsp";
        String functionJS = "open";
        String indexs = request.getParameter("index");
        int index = 0;

        //Controlli sui parametri
        if (indexs != null) {
            index = Integer.parseInt(indexs);
        }
        if (direction == null) {
            direction = "ASC";
        }

        //Paginator
        Paginator paginator = new Paginator();
        paginator.setCSSClass("pager-list");
        paginator.setCSSClassCurrentPage("selected");
        paginator.setCurrentIndex(index);
        paginator.setElementsForPage_fromDB(true);
        paginator.setFunctionJS(functionJS);
        paginator.setPageReturn(jsp);

        //Set Parametri iniziali
        paginator.addParameter("index", index + "");

        List<Utente> list = new LinkedList<Utente>();
        if (order == null) {
            Query q = em.createNamedQuery("Utente.findAll", Utente.class);
            paginator.setTotalElements(q.getResultList().size());
            q.setFirstResult(index);
            q.setMaxResults(paginator.getElementsForPage());
            list = q.getResultList();
        } else {
            if (direction.equals("ASC")) {
                paginator.addParameter("direction", "ASC");
                Query q = em.createQuery("SELECT p FROM Utente p ORDER BY p." + order + " ASC", Utente.class);
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            } else {
                paginator.addParameter("direction", "DESC");
                Query q = em.createQuery("SELECT p FROM Utente p ORDER BY p." + order + " DESC", Utente.class);
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            }

        }

        Iterator i = list.iterator();
        out.print("<table class=\"listing\" border=\"0\">");
        out.print("<tr>");
        out.print("<td>" + paginator.getFunction("order", "user", "Username") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "nome", "Name") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "cognome", "Surname") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "tipo", "Type") + "</td>");
        out.print("<td style=\"min-width: 100px\" colspan=\"1\">Fast Setting</td>");
        out.print("</tr>");
        Utente admin = (Utente) session.getAttribute("admin");
        while (i.hasNext()) {
            Utente p = (Utente) i.next();

            out.print("<tr id=\"user_" + p.getIdutente() + "\"  >");
            out.print("<td class=\"td-name\"><a href=\"javascript:open('mod_user.jsp',{uid:'" + p.getIdutente() + "'});\">" + p.getUser() + "</a></td>");
            out.print("<td class=\"td-code\">" + p.getNome() + "</td>");
            out.print("<td class=\"td-code\">" + p.getCognome() + "</td>");
            out.print("<td class=\"td-code\">" + p.getTipo() + "</td>");
            out.print("<td class=\"td-fast-setting\"><a class=\"delete-user\" href=\"./center/del_user_preselect.jsp?uid=" + p.getIdutente() + "&admin_id=" + admin.getIdutente() + "\"><img id=\"delete_prod_" + p.getIdutente() + "\" src=\"../engine/img/delete.png\"></img></a></td>");
            out.print("</tr>");
        }
        out.print("</table>");

        //!!!IMPORTANTE!!!! rimettere la direzione in quella della pagina attuale
        paginator.addParameter("direction", direction);
        out.print(paginator);

        em.close();
        //emf.close();
%>
    <div class="response"></div>
</div>

<div id="otheradmin" style="display:none">
    <select id="ot_admin">
        <%
            emf = PersistenceManager.getInstance().getEntityManagerFactory();
            em = emf.createEntityManager();

            List<Utente> listAll = new LinkedList<Utente>();
            Query qAll = em.createQuery("SELECT p FROM Utente p", Utente.class);
            listAll = qAll.getResultList();
            Iterator<Utente> itAll = listAll.iterator();
            while (itAll.hasNext()) {
                Utente temp = itAll.next();
                out.println("<option value=\"" + temp.getIdutente() + "\">" + temp.getNome() + " " + temp.getCognome() + " (" + temp.getUser() + ")" + "</option>");
            }

            em.close();
        %>
    </select>
</div>

<script>
    $("tr").hover(
    function(){
        $(this).find(".td-name").append($("<div class=\"tr-edit\">EDIT<div>"));
    },
    function () {
        $(this).find(".td-name .tr-edit").remove();
    }
);
</script>